! ***********************    ***********************    ***********************
! 3/3/16  This job just gets rid of the stocks that are too small -- and sets
!         up the data to be read in for the bootstrap.
!         The codes that build the optimal portfolios for the sample can work with all
!         of the data, and automatically cull the sample, but no need to complicate the
!         bootstrap code.
! ***********************     ***********************    ***********************
USE SVRGN_INT
IMPLICIT NONE
DOUBLE PRECISION, DIMENSION(5000,10) :: fchars
DOUBLE PRECISION, DIMENSION(5000) :: fweig,fsweig,MINSIZ,frit
DOUBLE PRECISION :: rdum,betdum,rsddum,lszdum,bmdum,momdum,afydum,lysdum,dumome,cpi
INTEGER, DIMENSION(5000) :: ffin
INTEGER :: perdum,T,i,jm,ljm,j,jp,jmdum,fktr,is,iostat,errcod,status,finind,datg,TSTKS
CHARACTER (len=8) :: garb
OPEN(unit= 14, file='/Users/lamlap/BSV/2022/data/BSVUSEALLSIC22.dat', STATUS ='OLD',iostat= errcod)
open(unit= 16, file='/Users/lamlap/BSV/2022/data/deflat22.csv', status ='old',iostat= errcod)
OPEN(unit= 24, file='/Users/lamlap/BSV/2022/data/BSVUSE22Jul23.dat', STATUS ='REPLACE')
OPEN(unit= 26, file='/Users/lamlap/BSV/2022/data/sampleinfo.dat', STATUS ='REPLACE')
TSTKS=0
T=744                         ! Through 2021.
DO i=1,157
   READ (16,*) garb          ! First line is header.  19600101 is on Row 158. 20220101 is on Row 901.
!   PRINT 1665, i, garb; 1665 format (2x,i3,2x,a8)
END DO
DO i=1,T
   READ (16,*) DATG,CPI,MINSIZ(i)   ! 50 million in Jan 1990 deflated in US $ to the current month
!   PRINT 1666, i,datg,minsiz(i); 1666 format (2x,i3,2x,i8,2x,f16.2)
   MINSIZ(i)=MINSIZ(i)*.1d-2    ! Mkt cap in the data file is in $1,000s.
END DO
DO
   READ (14,2166,iostat=status) perdum,jmdum,rdum,betdum,rsddum,lszdum,bmdum,momdum,afydum,lysdum,dumome,finind
            2166 format (i5,2x,i3,1x,f12.6,1x,f9.6,1x,f11.6,1x,f12.6,1x,f14.6,1x,f12.6,1x,f11.6,1x,f11.6,1x,f17.4,1x,i1)
   IF(status .ne. 0)  EXIT
   TSTKS=TSTKS+1
   jm=jmdum-60
   IF (dumome < MINSIZ(jm)) CYCLE
   IF (jm /= ljm) THEN
      IF (jm /= 1) THEN
         PRINT 78, ljm,TSTKS,fktr; 78 format (' Month ',i3,' Total Stocks: ',i5,3x,i4,' Stocks exceed the min size.')
         WRITE (26,786) ljm,TSTKS,fktr; 786 format (i3,2(2x,i5))
         CALL SVRGN(fweig,fsweig,N=fktr)
         IF (jm < 217) THEN             ! Through 1978
            is=1+int(.1*fktr)
!              is=1
         ELSE
            is=1+int(.2*fktr)
!              is=1
         END IF
         DO j=1,fktr
            IF (fweig(j) >= fsweig(is)) THEN
               WRITE (24,2466) ljm,frit(j),(fchars(j,jp),jp=1,7),ffin(j),fweig(j)
            2466 format (i3,1x,8(f12.6,1x),i1,1x,f17.4)
            END IF
         END DO
      END IF
      fktr=1
      TSTKS=0
   ELSE
      fktr=fktr+1
   END IF
   frit(fktr) = rdum
   fweig(fktr)= dumome
   fchars(fktr,1)=momdum
   fchars(fktr,2)=bmdum
   fchars(fktr,3)=lszdum
   fchars(fktr,4)=betdum
   fchars(fktr,5)=lysdum
   fchars(fktr,6)=afydum
   fchars(fktr,7)=rsddum
   ffin(fktr)=finind
   ljm=jm
END DO
!
! Set up the last month:
!
PRINT 78, ljm,fktr
      WRITE (26,786) ljm,TSTKS,fktr
      CALL SVRGN(fweig,fsweig,N=fktr)
      is=1+int(.2*fktr)
      DO j=1,fktr
         IF (fweig(j) >= fsweig(is)) THEN
            WRITE (24,2466) ljm,frit(j),(fchars(j,jp),jp=1,7),ffin(j),fweig(j)
         END IF
       END DO
STOP; END
